package org.csu.storemanage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.storemanage.mapper.UserMapper;
import org.csu.storemanage.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
@MapperScan("org.csu.storemanage.mapper")
public class UserService {

    //用户管理服务
    private final UserMapper userMapper;
    @Autowired
    public UserService(UserMapper userMapper)//构造注入
    {
        this.userMapper = userMapper;
    }

    public User findUserByUsername(@Param("username") String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectOne(queryWrapper);
    }

    public User findUserByPhone(@Param("phone") String phone){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectOne(queryWrapper);
    }

    public boolean isUsernameExist(@Param("username") String username)
    {
        return findUserByUsername(username) != null;
    }

    public boolean isPhoneExist(@Param("phone") String phone) {
        return findUserByPhone(phone) != null;
        }

   public int insertUser(@Param("user") User user){
        //密码MD5加密
       user.setPassword(md5(user.getPassword()));
       userMapper.insert(user);
       return findUserByUsername(user.getUsername()).getUser_id();
   }

   public List<User> findAllUser()
   {
       return userMapper.selectList(new QueryWrapper<>());
   }

    public void changeUserPasswordByUid(@Param("user_id")int user_id,@Param("password")String password)
    {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        //密码MD5加密
        password=md5(password);
        updateWrapper.eq("user_id",user_id).set("password",password);
        userMapper.update(null,updateWrapper);

    }

    public String changeUserPasswordByUsername(@Param("username")String username,@Param("password")String password)
    {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        //密码MD5加密
        password=md5(password);
        updateWrapper.eq("username",username).set("password",password);
        userMapper.update(null,updateWrapper);
        return password;
    }

    public void deleteUserByUsername(@Param("username")String username)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Username",username);
        userMapper.delete(queryWrapper);
    }

    public String resetUserPassword(@Param("username") String username){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        String password = username + String.valueOf(System.currentTimeMillis()%10000);
        updateWrapper.eq("username",username).set("password",md5(password));
        userMapper.update(null,updateWrapper);
        return password;
    }

    public static String md5(String password){
        //生成一个md5加密器
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            //计算MD5 的值
            md.update(password.getBytes());

            return new BigInteger(1, md.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

}
